package com.shunwang.errors.errorinfo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ErrorInfo {

	public static final String Ident = "com.shunwang.errors";
    private String objectId; //标识
    private String resource; //资源
    private String activity; //处理步骤
    private String moreInfo; //更多处理信息
    private Throwable cause; //错误信息
    private boolean hasMore = true;
    private boolean hasCause = true;
    private boolean hasDetails = true;
    private List<ErrorObject> details = new ArrayList<ErrorObject>();
    
    public ErrorInfo(String author,String resource){
        this.objectId = author;
        this.resource = resource;
    }
    
    public ErrorInfo(String author){
        this(author,null);
    }
    
    public ErrorInfo(){
        
    }
    
    public boolean hasDetails(){
        return details.size() > 0?true:false;
    }
    
    public String toString() {
        StringBuffer message = new StringBuffer();
        
        // object
        if (objectId != null) {
          message.append("  \n--- The error occurred in ");
          message.append(objectId);
          message.append(".");
        }
        // resource
        if (resource != null) {
          message.append("  \n--- Check the resource");
          message.append(resource);
          message.append(".");
        }
        // activity
        if (activity != null) {
          message.append("  \n--- The error occurred while ");
          message.append(activity);
          message.append(".");
        }

        // more info
        if (moreInfo != null && hasMore) {
          message.append("  \n--- ");
          message.append(moreInfo);
        }
        if(details.size() > 0 && hasDetails){
            message.append(" \n--- The error details ");
            for(int i=0;i<details.size();i++){
                ErrorObject errs = details.get(i);
                message.append(errs.toString());
                message.append(". \n");
            }
        }
        // cause
        if (cause != null && hasCause) {
          message.append("  \n--- Cause: ");
          message.append(cause.toString());
        }
        return message.toString();
      }

    public String getObjectId() {
        return objectId;
    }

    public void setObjectId(String objectId) {
        this.objectId = objectId;
    }

    public String getResource() {
        return resource;
    }

    public void setResource(String resource) {
        this.resource = resource;
    }

    public String getActivity() {
        return activity;
    }

    public void setActivity(String activity) {
        this.activity = activity;
    }

    public String getMoreInfo() {
        return moreInfo;
    }

    public void setMoreInfo(String moreInfo) {
        this.moreInfo = moreInfo;
    }

    public Throwable getCause() {
        return cause;
    }

    public void setCause(Throwable cause) {
        this.cause = cause;
    }

    public boolean isHasMore() {
        return hasMore;
    }

    public void setHasMore(boolean hasMore) {
        this.hasMore = hasMore;
    }

    public boolean isHasCause() {
        return hasCause;
    }

    public void setHasCause(boolean hasCause) {
        this.hasCause = hasCause;
    }

    public boolean isHasDetails() {
        return hasDetails;
    }

    public void setHasDetails(boolean hasDetails) {
        this.hasDetails = hasDetails;
    }
    
    public void addDetails(ErrorObject obj){
        details.add(obj);
    }
    
    public void removeDetails(ErrorObject obj){
        details.remove(obj);
    }
}
